package com.nft.service.school.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nft.constant.response.ResponseConstants;
import com.nft.paging.Paginator;
import com.nft.po.NftResponse;
import com.nft.service.school.entity.*;
import com.nft.service.school.mapper.*;
import com.nft.service.school.req.SchoolStudentReq;
import com.nft.service.school.resp.SchoolStudentResp;
import com.nft.service.school.service.SchoolStudentService;
import com.nft.utils.query.QueryUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class SchoolStudentServiceImpl extends ServiceImpl<SchoolStudentMapper, SchoolStudentEntity> implements SchoolStudentService {
    private static Logger logger = LoggerFactory.getLogger(SchoolStudentServiceImpl.class);

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SchoolClassesMapper schoolClassesMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SchoolPayrecordMapper schoolPayrecordMapper;
    @Autowired
    private SchoolConsumeMapper schoolConsumeMapper;

    @Override
    public NftResponse<List<SchoolStudentResp>> getStudentList(SchoolStudentReq query) {
        QueryWrapper<SchoolStudentEntity> wrapperQuery = new QueryWrapper<>();
        wrapperQuery.eq(QueryUtil.camelToUnderline("schoolId"), query.getSchoolId());
        wrapperQuery.eq(QueryUtil.camelToUnderline("classesId"), query.getClassesId());
        List<SchoolStudentEntity> records = this.baseMapper.selectList(wrapperQuery);
        List<SchoolStudentResp> resultList = BeanUtil.copyToList(records, SchoolStudentResp.class);
        for (SchoolStudentResp obj:resultList) {
            SysUserEntity student = sysUserMapper.selectById(obj.getStudentId());
            obj.setStudentName(null==student?"":student.getUserName());
            QueryWrapper<SchoolPayrecordEntity> wrapperQueryPay = new QueryWrapper<>();
            wrapperQueryPay.eq(QueryUtil.camelToUnderline("schoolId"), query.getSchoolId());
            wrapperQueryPay.eq(QueryUtil.camelToUnderline("studentId"), obj.getStudentId());
            List<SchoolPayrecordEntity> payList = schoolPayrecordMapper.selectList(wrapperQueryPay);
            if (null!=payList&&payList.size()>0) {
                SchoolPayrecordEntity payRecord = payList.get(0);
                obj.setRemainder(payRecord.getRemainder());
            }
        }
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "获取成功", resultList);
    }

    @Override
    public NftResponse<Paginator<SchoolStudentResp>> getStudentConsumeList(SchoolStudentReq query) {
        if (query.getPageNo() == null || query.getPageNo() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参页码错误", null);
        }
        if (query.getPageSize() == null || query.getPageSize() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参每页数量错误", null);
        }
        QueryWrapper<SysUserEntity> wrapperUser = new QueryWrapper<>();
        wrapperUser.eq(QueryUtil.camelToUnderline("phonenumber"), query.getPhone());
        wrapperUser.eq(QueryUtil.camelToUnderline("delFlag"),"0");
        wrapperUser.eq(QueryUtil.camelToUnderline("status"), "0");
        List<SysUserEntity> users = sysUserMapper.selectList(wrapperUser);
        List<Long> schoolIds = new ArrayList<>();
        for (SysUserEntity user:users) {
            schoolIds.add(user.getDeptId());
        }
        QueryWrapper<SchoolStudentEntity> wrapperQuery = new QueryWrapper<>();
        wrapperQuery.in(QueryUtil.camelToUnderline("schoolId"), schoolIds);
        wrapperQuery.orderByDesc("create_time");
        Paginator<SchoolStudentResp> paginator = new Paginator<SchoolStudentResp>(query.getPageNo(), query.getPageSize(), 0, new ArrayList<>());
        Page<SchoolStudentEntity> resultPage = this.baseMapper.selectPage(new Page<>(query.getPageNo(),query.getPageSize()),wrapperQuery);
        List<SchoolStudentResp> pageResult = BeanUtil.copyToList(resultPage.getRecords(), SchoolStudentResp.class);
        for (SchoolStudentResp obj:pageResult) {
            SchoolClassesEntity classes = schoolClassesMapper.selectById(obj.getClassesId());
            SysUserEntity teacherJw = sysUserMapper.selectById(classes.getJwteacherId());
            SysUserEntity teacherJx = sysUserMapper.selectById(classes.getJxteacherId());
            SysUserEntity student = sysUserMapper.selectById(obj.getStudentId());
            obj.setClassesName(null==classes?"":classes.getClassesName());
            obj.setTeacherNameJw(null==teacherJw?"":teacherJw.getUserName());
            obj.setTeacherNameJx(null==teacherJx?"":teacherJx.getUserName());
            obj.setConsumeAmount(null==classes?BigDecimal.valueOf(0):classes.getAmount());
            obj.setStudentName(null==student?"":student.getUserName());

            QueryWrapper<SchoolPayrecordEntity> wrapperQueryPay = new QueryWrapper<>();
            wrapperQueryPay.in(QueryUtil.camelToUnderline("schoolId"), schoolIds);
            wrapperQueryPay.eq(QueryUtil.camelToUnderline("studentId"), obj.getStudentId());
            List<SchoolPayrecordEntity> payList = schoolPayrecordMapper.selectList(wrapperQueryPay);
            if (null!=payList&&payList.size()>0) {
                SchoolPayrecordEntity payRecord = payList.get(0);
                obj.setRemainder(payRecord.getRemainder());
            }
            QueryWrapper<SchoolConsumeEntity> wrapperQueryCos = new QueryWrapper<>();
            wrapperQueryCos.in(QueryUtil.camelToUnderline("schoolId"), schoolIds);
            wrapperQueryCos.eq(QueryUtil.camelToUnderline("classesId"), obj.getClassesId());
            wrapperQueryCos.eq(QueryUtil.camelToUnderline("studentId"), obj.getStudentId());
            List<SchoolConsumeEntity> cosList = schoolConsumeMapper.selectList(wrapperQueryCos);
            obj.setNum(cosList.size());
        }
        paginator.setPageNo(resultPage.getCurrent());
        paginator.setPageSize(resultPage.getSize());
        paginator.setTotalDataNum(resultPage.getTotal());
        paginator.setPageData(pageResult);
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "获取成功", paginator);
    }
}
